package com.wx.wxapp.service;

import com.wx.wxapp.dao.*;
import com.wx.wxapp.entity.Activity;
import com.wx.wxapp.entity.Activityjoined;
import com.wx.wxapp.entity.Pet;
import com.wx.wxapp.entity.Petimage;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ActivityService {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /*
     *   功能：创建活动
     *   参数：
     *       title:活动标题
     *       address:活动地址
     *       information:活动信息
     *       petId:宠物id
     * */
    public void CreateActivity(String title,String address,String information,String time, int petId){
        System.out.println(address);
        System.out.println(information);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        ActivityjoinedMapper activityjoinedMapper = sqlSession.getMapper(ActivityjoinedMapper.class);

        if(title == null || address == null || information == null){
            System.out.println("test");
            //抛出异常
        }else {
            String id = UUID.randomUUID().toString();
            Activity activity = new Activity();
            Activityjoined activityjoined = new Activityjoined();
            activity.setId(id);activity.setTitle(title);activity.setAddress(address);activity.setInformation(information);activity.setTime(time);
            activityjoined.setActivityid(id);activityjoined.setPetid(petId);
            activityMapper.insert(activity);
            activityjoinedMapper.insert(activityjoined);
            sqlSession.close();
        }
    }

    /*
     *   功能：参加活动
     *   参数：
     *       activityId:活动id
     *       petId:宠物Id
     * */
    public void JoinActivity(String activityId,int petId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ActivityjoinedMapper activityjoinedMapper = sqlSession.getMapper(ActivityjoinedMapper.class);
        if (activityId == null){
            System.out.println("test");
        }else {
            Activityjoined activityjoined = new Activityjoined();
            activityjoined.setActivityid(activityId);activityjoined.setPetid(petId);
            activityjoinedMapper.insert(activityjoined);
            sqlSession.close();
        }
    }

    /*
     *   功能：退出活动
     *   参数：
     *       activityId:活动id
     *       petId:宠物Id
     * */
    public void ExitActivity(int petId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ActivityjoinedMapper activityjoinedMapper = sqlSession.getMapper(ActivityjoinedMapper.class);
        activityjoinedMapper.deleteByPetId(petId);
        sqlSession.close();
    }


    /*
     *   功能：获取活动详细信息
     *   参数：
     *       activityId:活动Id
     * */
    public Map GetActivity(String activityId){
        Map map = new HashMap();
        String image;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ActivityMapper activityMapper = sqlSession.getMapper(ActivityMapper.class);
        ActivityjoinedMapper activityjoinedMapper = sqlSession.getMapper(ActivityjoinedMapper.class);
        PetimageMapper petimageMapper = sqlSession.getMapper(PetimageMapper.class);
        PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        map.put("activity",activity);
        List<Activityjoined> activityjoineds = activityjoinedMapper.selectByActivityId(activityId);
        List<List<String>> lists = new ArrayList<>();
        for(Activityjoined activityjoined : activityjoineds){
            List<String> list = new ArrayList<>();
            int petId = activityjoined.getPetid();
            Pet pet = petMapper.selectByPrimaryKey(petId);
            list.add(pet.getName());list.add(pet.getSex());
            int userId = petMapper.selectByPrimaryKey(petId).getHost();
            List<Petimage> petimages = petimageMapper.selectByUserId(userId);
            if(!petimages.isEmpty()){
                image = petimages.iterator().next().getImagename();
                list.add(image);
            }
            lists.add(list);
        }
        map.put("pet",lists);
        sqlSession.close();
        return map;
    }

    public String GetPetActivityId(int petId){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ActivityjoinedMapper activityjoinedMapper = sqlSession.getMapper(ActivityjoinedMapper.class);
        Activityjoined activityjoined = activityjoinedMapper.selectByPetId(petId);
        if (activityjoined != null){
            sqlSession.close();
            return activityjoined.getActivityid();
        }else {
            sqlSession.close();
            return null;
        }
    }
}
